package com.cjl.demo.thread;

import java.util.ArrayList;

/**
 * <p>Description: </p>
 *
 * @ uthor chenjinliang
 * @ date: 2020/7/14
 */
public class TheadDemo implements Runnable {

    int n = 0;
    private int threadNum;


    public TheadDemo(int threadNum, int n){
        this.threadNum = threadNum;
        this.n = n;
    }

    public TheadDemo(){
    }

    @Override
    public void run(){
        while(n < 100){
            synchronized(TheadDemo.class){
                if(n % 3 == this.threadNum && n < 100){
                    System.out.println("thraed" + threadNum + ":" + n++);
                    TheadDemo.class.notifyAll();
                }else{
                    try{
                        TheadDemo.class.wait();
                    }catch (Exception e){

                    }
                }

            }
        }
    }

    /**
     * 常规思路：加锁 -> 不满足条件wait -> 满足条件执行并notifyAll
     */
    private Runnable sync1() {
        return () -> {
            int nameI = Integer.parseInt(Thread.currentThread().getName());
            while (n <= 100) {
                synchronized (TheadDemo.class) {
                    if (n % 3 == nameI && n <= 100) {
                        System.out.println("Thread-" + nameI + ":" + n++);
                        TheadDemo.class.notifyAll();
                    } else {
                        try {
                            TheadDemo.class.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
    }

    /**
     * 省掉不必要的wait()，notifyAll()
     */
    private Runnable sync2() {
        return () -> {
            int nameI = Integer.parseInt(Thread.currentThread().getName());
            while (n <= 100) {
                synchronized (TheadDemo.class) {
                    if (n % 3 == nameI) {
                        System.out.println("Thread-" + nameI + ":" + n++);
                    }
                }
            }
        };
    }

    /**
     * 不显示加锁 - 问题所在？此处难道是锁粗化，那为什么把n++放在打印里面又出问题
     */
    public Runnable sync3() {
        return () -> {
            int nameI = Integer.parseInt(Thread.currentThread().getName());
            while (n <= 1000) {
                if (n % 3 == nameI) {
                    System.out.println("Thread-" + nameI + ":" + n);
                    n++;
                }
            }
        };
    }


//    public static void main(String[] args) {
//
//        TheadDemo z = new TheadDemo();
////        Runnable r = z.sync1();
//        Runnable r = z.sync2();
////        Runnable r = z.sync3();
//
//        new Thread(r, "0").start();
//        new Thread(r, "1").start();
//        new Thread(r, "2").start();
//    }

    public static void main(String[] args) {
        int num = 100;
        TheadDemo demo1 = new TheadDemo(0, 100);
        TheadDemo demo2 = new TheadDemo(1, 100);
        TheadDemo demo3 = new TheadDemo(2, 100);
        new Thread(demo1).start();
        new Thread(demo2).start();
        new Thread(demo3).start();
//        ArrayList
    }

    public String trans(int n){
        String [] keyArray = {"A","B","Z"};
        String result = null;
        do{
            result = keyArray[n%26] + result;
            n = n/26;
        }while(n !=0);
        return result;

    }
}
